//
//  ITPageViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/18.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITPageViewController.h"
#import "ITPageSubViewController.h"

@interface ITPageViewController ()<UIPageViewControllerDelegate, UIPageViewControllerDataSource>

@property (nonatomic, strong) UISegmentedControl *segmentControl;
@property (nonatomic, strong) UIPageViewController *pageViewController;
@property (nonatomic, strong) NSArray *pageContentArray;
@end

@implementation ITPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSMutableArray *mutableArr = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        NSString *contentString = [[NSString alloc] initWithFormat:@"This is the page %d of content displayed using UIPageViewController", i];
        [mutableArr addObject:contentString];
    }
    self.pageContentArray = [mutableArr copy];
    
    [self setupViews];
}

-(void)viewWillLayoutSubviews {

    self.segmentControl.frame = CGRectMake(0, 88, self.view.bounds.size.width, 44);
    self.pageViewController.view.frame = CGRectMake(0, 88 + 44, self.view.bounds.size.width, self.view.bounds.size.height - 88 - 44);;

}

-(void)setupViews {
    self.segmentControl = [[UISegmentedControl alloc] initWithItems:@[@"标签一",@"标签二",@"标签三",@"标签四",@"标签五"]];
    [self.segmentControl addTarget:self action:@selector(segAction:) forControlEvents:UIControlEventValueChanged];
    [self.segmentControl setSelectedSegmentIndex:0];
    [self.view addSubview:self.segmentControl];
    
//    两个page之间的间距，只有UIPageViewControllerTransitionStyleScroll格式才生效
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:0] forKey: UIPageViewControllerOptionInterPageSpacingKey];
    self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
    
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];

    ITPageSubViewController *initialViewController0 = [self viewControllerAtIndex:0];// 得到第一页
    ITPageSubViewController *initialViewController1 = [self viewControllerAtIndex:1];// 得到第二页
    NSArray *viewControllers = @[initialViewController0];

    [self.pageViewController setViewControllers:viewControllers
     direction:UIPageViewControllerNavigationDirectionReverse
      animated:NO
    completion:nil];

}

-(void)segAction:(UISegmentedControl *)control {
    ITPageSubViewController *temp = self.pageViewController.viewControllers.firstObject;
    NSUInteger index = [self indexOfViewController:temp];
    ITPageSubViewController *initialViewController = [self viewControllerAtIndex:control.selectedSegmentIndex];
    NSArray *viewControllers = @[initialViewController];
    if (index > control.selectedSegmentIndex) {
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionReverse animated:YES completion:nil];
    } else {
        [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    }
    
}

#pragma mark - 根据index得到对应的UIViewController
 
- (ITPageSubViewController *)viewControllerAtIndex:(NSUInteger)index {
    if (([self.pageContentArray count] == 0) || (index >= [self.pageContentArray count])) {
        return nil;
    }
    // 创建一个新的控制器类，并且分配给相应的数据
    ITPageSubViewController *contentVC = [[ITPageSubViewController alloc] init];
    contentVC.content = [self.pageContentArray objectAtIndex:index];
    return contentVC;
}
 
#pragma mark - 数组元素值，得到下标值
 
- (NSUInteger)indexOfViewController:(ITPageSubViewController *)viewController {
    return [self.pageContentArray indexOfObject:viewController.content];
}

#pragma mark - UIPageViewControllerDelegate, UIPageViewControllerDataSource

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexOfViewController:(ITPageSubViewController *)viewController];
    if ((index == 0) || (index == NSNotFound)) {
        return nil;
    }
    index--;
    return [self viewControllerAtIndex:index];
}

- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSUInteger index = [self indexOfViewController:(ITPageSubViewController *)viewController];
    if (index == NSNotFound) {
        return nil;
    }
    index++;
    if (index == [self.pageContentArray count]) {
        return nil;
    }
    return [self viewControllerAtIndex:index];

}

- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<ITPageSubViewController *> *)pendingViewControllers {
    NSUInteger index = [self indexOfViewController:(ITPageSubViewController *)pendingViewControllers.firstObject];
    [self.segmentControl setSelectedSegmentIndex:index];
    NSLog(@"content %@", pendingViewControllers.firstObject.content);
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<ITPageSubViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed {
    if (!completed) {
        NSUInteger index = [self indexOfViewController:(ITPageSubViewController *)previousViewControllers.firstObject];
        [self.segmentControl setSelectedSegmentIndex:index];
    }
    NSLog(@"finished %d, content %@, completed %d", finished, previousViewControllers.firstObject.content, completed);
}

@end
